package com.example.springBoot.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AdminInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		Long userId = (Long) session.getAttribute("userId");
		// 判断用户是否登陆成功，如果登陆成功继续下面的步骤，如果没有登陆成功返回登陆页面登陆
		if (username == null && userId == null) {
			// 页面跳转登陆
			response.sendRedirect(request.getContextPath() + "/login");
			return false;
		}
		return true;// 返回trun继续我们的请求，返回false我们的请求将会中断
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		// System.out.println("我们的额Controller方法请求之后会执行这个方法，我们测试看看！！！");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		// System.out.println("在整个请求结束之后被调用，也就是在DispatcherServlet
		// 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
	}

}
